/*
 *  filterMenus.js
 *
 *  A collection of methods for the application-specific FilterMenu object,
 *  including the constructor for the FilterMenu object. The methods
 *  initialize and populate the filter menus as rendered in the browser
 *  and set up listeners for the various filters of the user interface.
 *
 *  @author Tanya L. Crenshaw
 */
define(['./utilities'], function(utilities) 
{
	/* FilterMenu constructor
	 *
	 * @param name The name for the HTML element representing this FilterMenu object.
	 * @param idName The id for the HTML element representing this FilterMenu object.
	 * @param className The class for the HTML element representing this FilterMenu object.
	 * @param divId The HTML div element's id where the filter is to be placed.
	 * @param menuItems The object containing all the menu items with which to populate the menu.
	 * @param action The function to call when the menu changes.
	 * menu is to be placed.
	 */
	var FilterMenu = function(name, idName, className, divId, menuItems, action) 
	{
		this.name = name;
		this.idName = idName;
		this.className = className;
		this.divId = divId;
		this.menuItems = menuItems;
		this.action = action;

		this.createMenu = function() 
		{
			return createMenu.call(this);
		};
	};
	
	/*
	 * createMenu
	 *
	 * Create a select HTML element for the initialized FilterMenu object.
	 */
	var createMenu = function() 
	{
		// Get the HTML element to which this menu has been assigned.
		el = document.getElementById(this.divId);

		// Create a select element
		var select = document.createElement("select");

		// Set some attributes
		select.setAttribute("name", this.name);
		select.setAttribute("id", this.idName);

		// Create a default option, which we chose to be "State"
		option = document.createElement("option");
		option.setAttribute("value", "CEEB");
		option.setAttribute("label", "CEEB");
		select.appendChild(option);
		
		option = document.createElement("option");
		option.setAttribute("value", "High School Name");
		option.setAttribute("label", "High School Name");
		select.appendChild(option);
		
		option = document.createElement("option");
		option.setAttribute("value", "City");
		option.setAttribute("label", "City");
		select.appendChild(option);
		
		option = document.createElement("option");
		option.setAttribute("value", "State");
		option.setAttribute("label", "State");
		select.appendChild(option);
		
		option = document.createElement("option");
		option.setAttribute("value", "Zip Code");
		option.setAttribute("label", "Zip Code");
		select.appendChild(option);

		// Append the resulting select menu to the div assigned
		// to this object.
		el.appendChild(select);

		// Attach an action to the menu.
		// Attach a function to provide some syntax guides when the user wants to look
		// up by high school name.
		utilities.addEvent(document.getElementById(this.divId), 'change', function()
		{
			if (utilities.getLookupType() == "High School Name")
			{
				//Add a syntax message for the user to see
				utilities.getRightSidebarElement().innerHTML = "<p>Due to high repetition of high school names, this should be the format of the input for high school lookups:</p>";
				utilities.getRightSidebarElement().innerHTML += "<p>High School Name, State";
				
				//Also add preview text to the textbox.
				document.getElementById("lookup").setAttribute("value", "High School Name, State");
				document.getElementById("lookup").setAttribute("onFocus", "if (this.value=='High School Name, State') this.value = ''");
				document.getElementById("lookup").setAttribute("onBlur", "if (this.value=='') this.value = 'High School Name, State'");
			}
			else
			{
				//Any other type of lookup doesn't require assistance with the syntax
				document.getElementById("lookup").setAttribute("value", "");
			}
		});
	};
	
	// Any functions defined in this return statement are considered public
	// functions by RequireJS, and accessible through the namespace that
	// is attached to this module when loaded in other files.
	return {
		FilterMenu : FilterMenu
	};
});